import { ApiProperty } from '@nestjs/swagger'
import { Column, Entity, ManyToMany, Relation } from 'typeorm'

import { CompleteEntity } from '~/common/entity/common.entity'

import { RoleEntity } from '../role/role.entity'

@Entity({ name: 'system_menu' })
export class MenuEntity extends CompleteEntity {
  @Column({ type: 'varchar', length: 50, comment: '菜单名称' })
  @ApiProperty({ description: '菜单名称' })
  name: string

  @Column({ type: 'varchar', length: 100, nullable: true, default: '', comment: '权限标识' })
  @ApiProperty({ description: '权限标识' })
  permission: string

  @Column({ type: 'tinyint', comment: '菜单类型' })
  @ApiProperty({ description: '菜单类型' })
  type: number

  @Column({ type: 'int', default: 0, comment: '显示顺序' })
  @ApiProperty({ description: '显示顺序' })
  sort: number

  @Column({ name: 'parent_id', type: 'bigint', default: 0, comment: '父菜单ID' })
  @ApiProperty({ description: '父菜单ID' })
  parentId: number

  @Column({ type: 'varchar', length: 200, default: '', comment: '路由地址' })
  @ApiProperty({ description: '路由地址' })
  path: string

  @Column({ type: 'varchar', length: 100, default: '#', comment: '菜单图标' })
  @ApiProperty({ description: '菜单图标' })
  icon: string

  @Column({ type: 'varchar', length: 255, nullable: true, comment: '组件路径' })
  @ApiProperty({ description: '组件路径' })
  component: string

  @Column({ name: 'component_name', type: 'varchar', length: 255, nullable: true, comment: '组件名' })
  @ApiProperty({ description: '组件名' })
  componentName: string

  @Column({ type: 'tinyint', default: 0, comment: '菜单状态' })
  @ApiProperty({ description: '菜单状态' })
  status: number

  @Column({ type: 'bit', default: true, comment: '是否可见' })
  @ApiProperty({ description: '是否可见' })
  visible: boolean

  @Column({ name: 'keep_alive', type: 'bit', default: true, comment: '是否缓存' })
  @ApiProperty({ description: '是否缓存' })
  keepAlive: boolean

  @Column({ name: 'always_show', type: 'bit', default: true, comment: '是否总是显示' })
  @ApiProperty({ description: '是否总是显示' })
  alwaysShow: boolean

  @ManyToMany(() => RoleEntity, role => role.menus, {
    onDelete: 'CASCADE',
  })
  roles: Relation<RoleEntity[]>
}
